<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/12/18
 * Time: 14:38
 */
class Order extends MY_Admin_Controller
{
    function __construct(){
        parent::__construct();
        $this->load->helper('order');
        $this->config->load('order',true);
        $this->load->model('order/Orders_model');
        $this->assign('index_channel','contents');
        $this->assign('state_cn',$this->config->item('order')['state_cn']);
    }

    /**
     * 订单列表
     */
    function index(){
        $page = $this->input->get('page');
        $page = $page > 0? $page: 1;
        $status =  $this->input->get('status');
        $status = $status>0?$status:1;
        $condition = array(
            'is_del'=>0,
            'status'=>$status
        );
        $user_id = $this->input->get('user_id');
        $activity_id = $this->input->get('activity_id');
        $order_id = $this->input->get('order_id');
        if($user_id)
            $condition['user_id']=$user_id;
        if($activity_id)
            $condition['activity_id'] = $activity_id;
        if($order_id)
            $condition['order_id']=$order_id;
        $base_url = url_query($condition, '/m-admin/order/index');
        $join_conditions = array(
            array('table_name'=>'users','condition'=>'order.user_id=users.id','field'=>'username'),
            array('table_name'=>'activity','condition'=>'activity.id=order.activity_id','field'=>'title')
        );
        $pagination = $this->Orders_model->pagination_join($base_url, $page, $condition, 'order.id desc','*',20,$join_conditions);
        $this->session->set_userdata(
            'back_list_page',
            $this->get_self_url()
        );
        $this->assign('pagination', $pagination);
        $this->display();
    }

    /**
     * 订单详情
     * @param $id 表id
     */
    function info($id){
        $this->load->model('users/Users_model');
        $this->load->model('activities/Activities_model');
        $this->load->model('order/Order_serial');
        $this->load->model('order/Order_eligible_refund');
        $order = $this->Orders_model->getOne(array('id'=>$id));
        $user  = $this->Users_model->getOne(array('id'=>$order['user_id']));
        $activity = $this->Activities_model->getOne(array('id'=>$order['activity_id']));
        $order['serial'] = $this->Order_serial->getOne(array('order_id'=>$order['order_id']));
        $order['eligible_refund'] = $this->Order_eligible_refund->getOne(array('order_id'=>$order['order_id']));
        $this->assign('user',$user);
        $this->assign('activity',$activity);
        $this->assign('order',$order);
        $this->display();
    }
    /**
     * 支付流水
     */
    function serial(){
        $page = $this->input->get('page');
        $page = $page > 0? $page: 1;
        $this->load->model('order/Order_serial');
        $condition = array();
        $order_id = $this->input->get('order_id');
        if($order_id)
            $condition['order_id']=$order_id;
        $serial_id = $this->input->get('serial_number');
        if($serial_id)
            $condition['serial_number']=$serial_id;
        $base_url = url_query($condition, '/m-admin/order/serial');
        $pagination = $this->Order_serial->pagination($base_url, $page, $condition, 'id desc');
        $this->session->set_userdata(
            'back_list_page',
            $this->get_self_url()
        );
        $this->assign('pagination',$pagination);
        $this->display();
    }

    /**
     * 发生的退款信息
     */
    function refund(){
        $page = $this->input->get('page');
        $page = $page > 0? $page: 1;
        $this->load->model('order/Order_eligible_refund');
        $condition = array();
        $user_id = $this->input->get('user_id');
        $order_id = $this->input->get('order_id');
        $status = $this->input->get('status');
        $status = $status>0?$status:1;
        $condition['status'] = $status;
        if($user_id)
            $condition['user_id']=$user_id;
        if($order_id)
            $condition['order_id']=$order_id;

       $join_conditions = array(
            array('table_name'=>'users','condition'=>'eligible_refund.user_id=users.id','field'=>'username'),
        );
        $base_url = url_query($condition, '/m-admin/order/eligible_refund');
        $pagination = $this->Order_eligible_refund->pagination_join($base_url, $page, $condition, 'eligible_refund.id desc','*',20,$join_conditions);
        //        echo $this->order_eligible_refund->db->last_query();
       // x$pagination = $this->order_eligible_refund->pagination($base_url, $page, $condition, 'id desc');
        $this->session->set_userdata(
            'back_list_page',
            $this->get_self_url()
        );
        $this->assign('pagination',$pagination);
        $this->display();
    }

    /**
     *  客服手动退款  =》第二版改为自动  等待接口申请
     */
    function do_refund(){
        $id = $this->input->post('serial_id');
        if($id<1){
            $this->json_response(false,null,'id错误');
        }
        $order_id = $this->input->post('order_id');
        $this->load->model('order/Order_serial');
        $this->Order_serial->refund($id,$order_id);
        $this->json_response(true);
    }
    function do_refund_stop(){
        $id = $this->input->post('serial_id');
        if($id<1){
            $this->json_response(false,null,'id错误');
        }
        $this->load->model('order/Order_serial');
        $this->Order_serial->refund_stop($id);
        $this->json_response(true);
    }
}